﻿
''' <summary>
''' customer stock information
''' </summary>
''' <remarks></remarks>
<Serializable()>
Public Class clsMyStockInfo

#Region "property"
    Public Property StockID As String
        Get
            Return m_StockID
        End Get
        Set(value As String)
            m_StockID = value
        End Set
    End Property

    Public Property NumberOfShares As Integer
        Get
            Return m_NumberOfShares
        End Get
        Set(value As Integer)
            m_NumberOfShares = value
        End Set
    End Property

    Public Property CostPrice As Double
        Get
            Return m_CostPrice
        End Get
        Set(value As Double)
            m_CostPrice = value
        End Set
    End Property

    Public Property DateBuy As DateTime
        Get
            Return m_DateBuy
        End Get
        Set(value As DateTime)
            m_DateBuy = value
        End Set
    End Property

    Public ReadOnly Property Profit As Double '(current price - cost price)
        Get
            Return m_Profit
        End Get
    End Property

    Public ReadOnly Property ProfitPercentage As String '(current price - cost price)/cost price*100%
        Get
            Return m_ProfitPercentage
        End Get
    End Property

#End Region

#Region "method"
    ''' <summary>
    ''' recalculate customer's profit for this stock set according current price
    ''' </summary>
    ''' <param name="CurrentPrice"></param>
    ''' <remarks></remarks>
    Public Sub UpdateProfit(CurrentPrice As Double)
        m_Profit = CurrentPrice - m_CostPrice

        If CostPrice <> 0 Then
            m_ProfitPercentage = Format(m_Profit / m_CostPrice, "0.00%")
        Else
            m_ProfitPercentage = ""
        End If

    End Sub
#End Region



    Private m_StockID As String
    Private m_NumberOfShares As Integer
    Private m_CostPrice As Double
    Private m_DateBuy As DateTime
    Private m_Profit As Double '(current price - cost price)
    Private m_ProfitPercentage As String '(current price - cost price)/cost price*100%

   
End Class

Public Module modMyStocks
    

    Public Function LoadMyStockFromFile() As System.Collections.ObjectModel.Collection(Of clsMyStockInfo)
        Return LoadMyStockFromFile(MyStockDataFile)
    End Function

    Public Function LoadMyStockFromFile(ByVal strPath As String) As System.Collections.ObjectModel.Collection(Of clsMyStockInfo)
        Dim MyStockTmp As New System.Collections.ObjectModel.Collection(Of clsMyStockInfo)

        Try
            If My.Computer.FileSystem.FileExists(strPath) Then
                'load my stock from file
                Dim formatter As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
                Dim stream As System.IO.FileStream = New System.IO.FileStream(strPath, IO.FileMode.Open, IO.FileAccess.Read)

                MyStockTmp = CType(formatter.Deserialize(stream), System.Collections.ObjectModel.Collection(Of clsMyStockInfo))
                stream.Close()
            End If
        Catch ex As Exception
            Return MyStockTmp
        End Try
        Return MyStockTmp
    End Function

    Public Function SaveMyStockToFile(ByVal NewMyStock As System.Collections.ObjectModel.Collection(Of clsMyStockInfo)) As Boolean
        Return SaveMyStockToFile(NewMyStock, MyStockDataFile)
    End Function

    Public Function SaveMyStockToFile(ByVal NewMyStock As System.Collections.ObjectModel.Collection(Of clsMyStockInfo), ByVal strPath As String) As Boolean
        'save options to file
        Try
            Dim formatter As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            Dim stream As System.IO.FileStream = New System.IO.FileStream(strPath, IO.FileMode.Create, IO.FileAccess.Write)
            formatter.Serialize(stream, NewMyStock)
            stream.Flush()
            stream.Close()
        Catch ex As Exception
            Return False
        End Try

        Return True
    End Function

End Module
